{% extends "base.html" %}
{% block page_title %}题目分类管理 - {% endblock %}
{% block page_head %}
    <link rel="stylesheet" href="/static/library/jstree/themes/default/style.min.css">
    <script type="text/javascript" src="/static/library/jstree/jstree.min.js"></script>
{% endblock %}
{% block page_body %}
    <div class="header_container">
        <h1>题目分类管理</h1>
    </div>
    <div class="page_container">
        <div class="row">
            <div class="col-sm-5">
                <div class="table-responsive">
                    <div id="problem_classify_list" style="border-right: 1px solid #ccc; height: 100%;"></div>
                </div>
            </div>
            <div class="col-sm-7">
                <div id="editor_view"></div>
            </div>
        </div>
    </div>
{% endblock %}
{% block page_script %}
    <script type="text/javascript">
    $(function () {
       $("#problem_classify_list").on('changed.jstree', function (e, data) {
            if (data.selected.length>0){
                classify_id = data.instance.get_node(data.selected[0]).id;
                modify_classify(classify_id);
            }
       }).jstree({
            'core': {
                "multiple" : false,
                'data': {
                    "url": function (node) {
                        return node.id==="#" ? '{% url 'problem_get_classify_list' 0 %}' : '{% url 'problem_get_classify_list' %}';
                    },
                    "data": function (node) {
                        return { "id" : node.id };
                    }
                }
            }
       });
        function modify_classify(classify_id) {
            $.GRestP({
                url: "{% url 'problem_classify_editor' 0 %}".replace("0", classify_id),
                method: 'get',
                responseType: 'text',                   //设置jQuery的响应回调数据格式，默认为text，仅支持text和json的处理
                callback:function(flag, entity){
                    if(flag){
                        $("#editor_view").html(entity);
                        $("#problem_classify_modify_form").submit(function () {
                            $.GRestP({
                                responseType: "json",
                                callback: function (flag, entity) {
                                    if(flag){
                                        $.AlertBox({
                                            body: "分类名称修改成功",
                                            color: "success",
                                            title: "操作成功",
                                            callback: function(){
                                                $("#problem_classify_list").jstree('refresh');
                                            }
                                        }).show();
                                    }else{
                                        $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                                    }
                                }
                            }).submit_form(this);
                            return false;
                        });
                        $("#problem_classify_new_form").submit(function () {
                            $.GRestP({
                                responseType: "json",
                                callback: function (flag, entity) {
                                    if(flag){
                                        $.AlertBox({
                                            body: "新增分类成功",
                                            color: "success",
                                            title: "操作成功",
                                            callback: function(){
                                                $("#problem_classify_list").jstree('refresh');
                                            }
                                        }).show();
                                    }else{
                                        $.AlertBox({body: entity.msg, color: "danger", title: "操作失败" }).show();
                                    }
                                }
                            }).submit_form(this);
                            return false;
                        });
                        $("#problem_classify_delete_form").submit(function () {
                            $.ConfirmBox({
                                title: "提示",
                                body: "删除分类并删除其所有子分类，确定吗？题目不会被删除，但是会被永久移出分类。",
                                color: 'warning',
                                callback: function (flag) {
                                    if (!flag) {
                                        return false;
                                    }
                                    $.GRestP({
                                        responseType: "json",
                                        callback: function (flag, entity) {
                                            if(flag){
                                                $.AlertBox({
                                                    body: "分类删除成功",
                                                    color: "success",
                                                    title: "操作成功",
                                                    callback: function(){
                                                        $("#problem_classify_list").jstree('refresh');
                                                    }
                                                }).show();
                                            }
                                        }
                                    }).submit_form("#problem_classify_delete_form");
                                }
                            }).show();
                            return false;
                        });

                    }
                }
            }).call();
        }
    });
    </script>
{% endblock %}